package com.atguigu.tingshu.user.startagy.impl;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.startagy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component("1001")
@Slf4j
public class AlbumStrategy implements ItemTypeStrategy {
    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;


    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("专辑策略模式");
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        Long userId = userPaidRecordVo.getUserId();
        String orderNo = userPaidRecordVo.getOrderNo();
        Long count = userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getOrderNo, orderNo));
        if(count > 0){
            return;
        }
        //专辑
        if(!CollectionUtils.isEmpty(itemIdList)){
            Long albumId = itemIdList.get(0);
            UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
            userPaidAlbum.setOrderNo(orderNo);
            userPaidAlbum.setUserId(userId);
            userPaidAlbum.setAlbumId(albumId);
            userPaidAlbumMapper.insert(userPaidAlbum);
        }
    }
}
